<?php

namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

/**
 * Created by PhpStorm.
 * User: ycp
 * Date: 2020/3/14
 * Time: 10:33
 */

class UpdateStatus extends command
{
    protected function configure()
    {
        $this->setName('updateStatus')->setDescription('update activity and goods status');
    }

    protected function execute(Input $input, Output $output)
    {
        //自动更新活动状态
        $this->updateActivityStatus();

    }

    private function updateActivityStatus()
    {
        $activityList = Db::name('lite_activity')->where(['is_deleted' => 0])->whereIn('status', [0, 1])->column('id, apply_start_time, apply_end_time, status', 'id');
        $now = time();
        foreach ($activityList as $activityId => $activityInfo){
            //未开始状态
            $startTime = strtotime($activityInfo['apply_start_time']);
            $endTime = strtotime($activityInfo['apply_end_time']);
            if( $activityInfo['status'] == 0){
                if($endTime > $now && $now > $startTime){
                    Db::name('lite_activity')->where(['id' => $activityId])->update(['status' => 1]);
                }
                if($now > $endTime){
                    Db::name('lite_activity')->where(['id' => $activityId])->update(['status' => 2]);
                }
            }
            //开始状态
            if($activityInfo['status'] == 1){
                if($now > $endTime){
                    Db::name('lite_activity')->where(['id' => $activityId])->update(['status' => 2]);
                }
            }

        }

    }
}